package org.cliff.message.platform.demo.controller;

import org.cliff.message.platform.core.annotation.MessageConsumer;
import org.cliff.message.platform.demo.dto.OrderInstructionsXmlRequest;
import org.cliff.message.platform.demo.dto.SoapResponse;
import org.cliff.message.platform.demo.util.XmlHelper;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.DocumentException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 刘文
 * @date 2023/11/1
 */
@RestController
@RequestMapping("/soap")
@Slf4j
public class SoapController {

    @MessageConsumer
    @PostMapping("/simple")
    public SoapResponse simple(HttpServletResponse response, HttpServletRequest request) {

        OrderInstructionsXmlRequest orderInstructionsXmlRequest= null;
        try {
            orderInstructionsXmlRequest = XmlHelper.xmlToBean(request, "/Envelope/Body", OrderInstructionsXmlRequest.class);
        } catch (IOException | DocumentException e) {
            log.error("Parse failed. ", e);
        }
        SoapResponse soapResponse = new SoapResponse();
        soapResponse.setId(orderInstructionsXmlRequest.getRequestDate().get(0).getMessageId());
        soapResponse.setMessage(orderInstructionsXmlRequest.getRequestDate().get(0).getMessage());
        return soapResponse;
    }
}
